import Vue from 'vue'
import VueRouter from 'vue-router'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import {getToken} from '@/common/utils/auth.js'
import util from '@/common/utils/util.js'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // name: 'index',
    redirect: '/index'
  },
  {
    path: "/login",
    name: "login",
    component: () => import('../views/login/index'),
    meta: {
      title: '用户登录'
    }
  },
  {
    path: "/loginSuccess",
    name: "loginSuccess",
    component: () => import('../views/login/loginSuccess'),
    meta: {
      title: '用户微信登录'
    }
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/layout/tabs'),
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/home'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/system/user_manage',
        name: 'userManage',
        component: () => import('../views/system/user_manage/user_manage'),
        meta: {
          title: '用户管理'
        }
      },
      {
        path: '/system/role_manage',
        name: 'roleManage',
        component: () => import('../views/system/role_manage/role_manage'),
        meta: {
          title: '角色管理'
        }
      },
      {
        path: '/system/resource_manage',
        name: 'resourceManage',
        component: () => import('../views/system/resource_manage/resource_manage'),
        meta: {
          title: '资源管理'
        }
      },
      {
        path: '/base/go_department',
        name: 'goDepartment',
        component: () => import('../views/base/go_department/go_department'),
        meta: {
          title: '部门设置'
        }
      },

      {
        path: '/base/go_personnel',
        name: 'GoPersonnel',
        component: () => import('../views/base/go_personnel/go_personnel'),
        meta: {
          title: '人员管理'
        }
      },
      {
        path: '/base/go_photograph',
        name: 'goPhotograph',
        component: () => import('../views/base/go_photograph/go_photograph'),
        meta: {
          title: '照片管理'
        }
      },
      {
        path: '/task/go_place_to_register',
        name: 'goPlaceToRegister',
        component: () => import('../views/task/go_place_to_register/go_place_to_register'),
        meta: {
          title: '场所登记'
        }
      },
      {
        path: '/task/go_time_frame',
        name: 'goTimeFrame',
        component: () => import('../views/task/go_time_frame/go_time_frame'),
        meta: {
          title: '时段管理'
        }
      },
      {
        path: '/base/go_position',
        name: 'goPosition',
        component: () => import('../views/base/go_position/go_position'),
        meta: {
          title: '职位管理'
        }
      },
      {
        path: '/task/go_rank',
        name: 'goRank',
        component: () => import('../views/task/go_rank/go_rank'),
        meta: {
          title: '级别设置'
        }
      },
      {
        path: '/work/go_check_on_work_attendance',
        name: 'goCheckOnWorkAttendance',
        component: () => import('../views/work/go_check_on_work_attendance/go_check_on_work_attendance'),
        meta: {
          title: '考勤组管理'
        }
      },
      {
        path: '/work/go_classes',
        name: 'goClasses',
        component: () => import('../views/work/go_classes/go_classes'),
        meta: {
          title: '班次管理'
        }
      },
      {
        path: '/device/device_manage',
        name: 'deviceManage',
        component: () => import('../views/device/device_manage/device_manage'),
        meta: {
          title: '设备管理'
        }
      },
      {
        path: '/face_logs/staff_logs',
        name: 'staffLogs',
        component: () => import('../views/face_logs/staff_logs/staff_logs'),
        meta: {
          title: '员工打卡记录'
        }
      },
      {
        path: '/face_logs/stranger_logs',
        name: 'strangerLogs',
        component: () => import('../views/face_logs/stranger_logs/stranger_logs'),
        meta: {
          title: '陌生人来访记录'
        }
      },
      
      {
        path: '/work/go_classes_time',
        name: 'goClassesTime',
        component: () => import('../views/work/go_classes_time/go_classes_time'),
        meta: {
          title: '打卡时间'
        }
      },
      
      {
        path: '/app/go_epidemic_register',
        name: 'goEpidemicRegister',
        component: () => import('../views/app/go_epidemic_register/go_epidemic_register'),
        meta: {
          title: '疫情预登记'
        }
      },
      {
        path: '/app/go_performance_management',
        name: 'goPerformanceManagement',
        component: () => import('../views/app/go_performance_management/go_performance_management'),
        meta: {
          title: '疫情预登记'
        }
      },

    ]
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}

NProgress.configure({ showSpinner: false })

// 前置导航守卫
router.beforeEach((to, from, next) => {

  // 如果非登录component 则进行token 验证
  if(to.name != 'login' && to.name != 'loginSuccess') {
    let token = getToken()
    if(util.isEmpty(token)) {
      router.replace('/login')
      return
    }
  }

  NProgress.start();
  next();
})

// 后置导航守卫
router.afterEach((to, from) => {
  document.title = to.meta.title;
  NProgress.done();
})

export default router
